<#assign crud_columns = [] />
<#assign columns = table.columnList />
<#list columns as col>
    <#if !(col.columnName =='id' || col.columnName == 'created' || col.columnName == 'modified')>
    <#assign crud_columns = crud_columns + [col] />
    </#if>
</#list>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${cfg.baseMapperPackage}.${table.beanName}DAO">
	<!--查询字段-->
	<sql id="columns">
	<#list columns as col>
	    a.`${col.columnName}`<#if col_has_next>,</#if>
	</#list>
	</sql>
	
	<!--查询结果集-->
	<resultMap id="beanMap" type="${cfg.baseDbBeanPackage}.domain.${table.beanName}">
	<#list columns as col>
	    <result property="${col.propertyName}" column="${col.columnName}"/>
	</#list>
	</resultMap>
	
	<!--根据主键获取实体-->
	<select id="get" resultMap="beanMap">
	  SELECT
		<include refid="columns"/>
	  FROM
		${table.tableName} a
	  WHERE 
	    <#list table.pkColumnList as pk><#if pk_index gt 0> AND</#if> ${pk} = <#noparse>#{</#noparse>${table.pkPropertyList[pk_index]}<#noparse>}</#noparse></#list>
	</select>

	<#--<!-- 新增记录 &ndash;&gt;-->
	<#--<insert id="add" useGeneratedKeys="true" keyProperty="id" parameterType="${cfg.baseDbBeanPackage}.${table.beanName}Model">-->
	  <#--INSERT INTO ${table.tableName} -->
	    <#--(-->
		<#--<#list crud_columns as col>-->
	    <#--`${col.columnName}`<#sep>,-->
		<#--</#list>-->
		<#---->
	    <#--)-->
	   <#--VALUES -->
	   <#--(-->
		<#--<#list crud_columns as col>-->
	    <#--<#noparse>#{</#noparse>${col.propertyName}<#noparse>}</#noparse><#sep>,-->
		<#--</#list>-->
		<#---->
	   <#--)-->
	<#--</insert>-->
	<!-- 新增记录 -->
	<insert id="add" useGeneratedKeys="true" keyProperty="id" parameterType="${cfg.baseDbBeanPackage}.domain.${table.beanName}">
	  INSERT INTO ${table.tableName}
	    (
        <trim prefix="" prefixOverrides=",">
			<#list crud_columns as col>
				<if test="${col.propertyName} != null">
					,`${col.columnName}`
				</if>
			</#list>
		</trim>

	    )
	   VALUES
	   (
        <trim prefix="" prefixOverrides=",">
			<#list crud_columns as col>
				<if test="${col.propertyName} != null">
					,<#noparse>#{</#noparse>${col.propertyName}<#noparse>}</#noparse>
				</if>
			</#list>
		</trim>
	   )
	</insert>


    <!-- 批量新增记录 -->
    <insert id="addBatch" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List">
        INSERT INTO ${table.tableName}
        (
        <trim prefix="" prefixOverrides=",">
			<#list crud_columns as col>
						,`${col.columnName}`
			</#list>
        </trim>
        )
        VALUES
        <foreach item="item" collection="list" separator=",">
            (
            <trim prefix="" prefixOverrides=",">
               <#list crud_columns as col>
                   <if test="item.${col.propertyName} != null">
                       ,<#noparse>#{</#noparse>item.${col.propertyName}<#noparse>}</#noparse>
                   </if>
			   </#list>
            </trim>
            )
        </foreach>
    </insert>



<#--<!--更新实体信息&ndash;&gt;-->
    <#--<update id="update" parameterType="${cfg.baseDbBeanPackage}.${table.beanName}Model">-->
      <#--UPDATE-->
      	<#--${table.tableName}-->
      <#--SET-->
      <#--<#list crud_columns as col>-->
        <#--`${col.columnName}` = <#noparse>#{</#noparse>${col.propertyName}<#noparse>}</#noparse><#sep>,-->
      <#--</#list>-->

      <#--WHERE <#list table.pkColumnList as pk><#if pk_index gt 0> AND</#if> ${pk} = <#noparse>#{</#noparse>${table.pkPropertyList[pk_index]}<#noparse>}</#noparse></#list>-->
    <#--</update>-->
    
    <!--更新实体信息，null值字段不更新-->
    <update id="updateIgnoreNull" parameterType="${cfg.baseDbBeanPackage}.domain.${table.beanName}">
        UPDATE ${table.tableName} 
        <set>
            <trim prefix="" prefixOverrides=",">
			  <#list crud_columns as col>
			  <if test="${col.propertyName} != null">
			   ,`${col.columnName}` = <#noparse>#{</#noparse>${col.propertyName}<#noparse>}</#noparse>
			  </if>
			  </#list>
            </trim>
        </set>
        WHERE  <#list table.pkColumnList as pk><#if pk_index gt 0> AND</#if> ${pk} = <#noparse>#{</#noparse>${table.pkPropertyList[pk_index]}<#noparse>}</#noparse></#list>
    </update>
    
    <!--根据主键删除实体-->
    <delete id="delete">
      DELETE FROM ${table.tableName} WHERE <#list table.pkColumnList as pk><#if pk_index gt 0> AND</#if> ${pk} = <#noparse>#{</#noparse>${table.pkPropertyList[pk_index]}<#noparse>}</#noparse></#list>
    </delete>


    <sql id="listClause">
		<#list crud_columns as col>
			  <if test="${col.propertyName} != null">
                  and `${col.columnName}` = <#noparse>#{</#noparse>${col.propertyName}<#noparse>}</#noparse>
              </if>
		</#list>
    </sql>

    <!--模糊查询-->
    <select id="findByConditionLike" resultMap="beanMap">
        SELECT
        <include refid="columns"/>
        FROM
		${table.tableName} a
        WHERE
        deleted = 0
        <include refid="listClause"/>
    </select>

    <!--普通查询-->
    <select id="findByCondition" resultMap="beanMap">
        SELECT
        <include refid="columns"/>
        FROM
		${table.tableName} a
        WHERE
        deleted = 0
        <include refid="listClause"/>
    </select>




</mapper>